package com.qf.java2201.dao.impl;

import com.qf.java2201.dao.UserinfoDao;
import com.qf.java2201.pojo.HUserinfo;
import com.qf.java2201.utils.DbUtils1;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class UserinfoDaoImpl implements UserinfoDao {
    QueryRunner queryRunner = new QueryRunner(DbUtils1.getDs());
    @Override
    public List<HUserinfo> findUserinfoByKsid(String tid) {
        String sql = "select * from h_userinfo where ksid = ?";

        List<HUserinfo> query = null;
        try {
            query = queryRunner.query(sql, new BeanListHandler<>(HUserinfo.class),tid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    @Override
    public HUserinfo findUserinfoByKdid(String did) {

        String sql = "select u.*,t1.tname as ks,t2.tname as zw, t3.tname as xl, t4.tname as zy from h_userinfo u join h_type t1 on u.ksid = t1.tid join h_type t2 on u.zwid = t2.tid join h_type t3 on u.xlid = t3.tid join h_type t4 on u.zyid = t4.tid where u.did = ?";

        HUserinfo query = null;
        try {
            query = queryRunner.query(sql, new BeanHandler<>(HUserinfo.class), did);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    @Override
    public int getDnum(String edid) {
        String sql = "select dnum from h_userinfo where did = ?";

        Integer query = null;
        try {
            query = queryRunner.query(sql, new ScalarHandler<Integer>(), edid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }
}
